/*  
  -- The Lime II Project -- 

  A tuplespaces-based middleware for coordinating agents and mobile hosts.
  Copyright (C) 2005.
  Gian Pietro Picco, Amy L. Murphy, Lorenzo Bellini.

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.

  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  Lesser General Public License for more details.

  You should have received this copy of the GNU Lesser General Public
  License along with this library; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 
*/

package lime2;

/**
 * The listener within a local weak reaction installed by the Lime system because of an in or rd towards a remote host
 * or the federated tuplespace.
 * 
 * @author Gian Pietro Picco
 * @author Amy Murphy
 * @author Lorenzo Bellini
 */

class SystemRemoteBlockingLW extends SystemListener
{
 // private data members	
 private boolean unspecified;
 private ReactionID id;
 private LimeTSMgr ltsm;
 private Result agent; 
  
 /**
  * Constructor method.
  * 
  * @param unspecified was the target of the RD/IN the whole federated tuplespace ?
  * @param id reaction identifier
  * @param ltsm LimeTSMgr of the tuplespace
  * @param agent result on which we synchronize
  */ 
 SystemRemoteBlockingLW(boolean unspecified, ReactionID id, LimeTSMgr ltsm, Result agent)
 { 	
  this.unspecified = unspecified;
  this.id = id;
  this.ltsm = ltsm;
  this.agent = agent;
 } 
  
 /**
  * This method is invoked automatically by the Lime system when the corresponding reaction must fire. 
  * 
  * @param re ReactionEvent
  */ 
 public void reactsToOut(ReactionEvent re)
 {
  // only if the RD/IN regards the federated tuplespace..
  if (unspecified) 
  {
   ReactionID[] ids = new ReactionID[] {id};	
   
   // the weak fired before the strong: we uninstall the strong
   ltsm.deregisterStrong(ids); 
 
   // we uninstall also the remote weaks since RD/IN are once reactions (please note that we use priority of events)
   ReactionOp tmp = new ReactionOp(LimeConstants.REMOVE_WEAK, ids, new Result());
   CommunicationMgr.get().postEvent(new RemReactionOp(LimeConstants.REM_REMOVE_MULTI, LimeConstants.OPEN, ltsm.getName(), tmp));
  }	
 
  // the tuple comes from remote as an event (IN doesn't need to take anything locally)
  agent.set(re.getTuple()); 
  agent.lock.release();  	
 } 	

}

